
using B.S.Data.ERP.API;
using CSRedis;
using ERP.Domain;
using ERP.infratrctrue;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using SqlSugar;
using System.Reflection;
using Yitter.IdGenerator;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers(x =>
{
    x.Filters.Add(typeof(ERPGuoFilters));
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
var RedisConnectionStrings = builder.Configuration.GetConnectionString("redis");
var redis = new CSRedisClient(RedisConnectionStrings);
RedisHelper.Initialization(redis);

//ע�������ģ�AOP������Ի�ȡIOC����������ֳɿ�ܱ���Furion���Բ�д��һ��
builder.Services.AddHttpContextAccessor();
//注册SqlSugar AddScoped
builder.Services.AddScoped<ISqlSugarClient>(s =>
{
    //Scoped的SqlSugarClient 
    SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
    {
        DbType = SqlSugar.DbType.MySql,
        ConnectionString = builder.Configuration.GetConnectionString("con"),
        IsAutoCloseConnection = true,
    },
   db =>
   {
       //每个SqlSugarClient实例的配置
       db.CodeFirst.InitTables<ChuKuInfo>();
       db.CodeFirst.InitTables<DingDan>();
   });
    return sqlSugar;
});

builder.Services.AddSwaggerGen(x =>
{
    var tran = AppDomain.CurrentDomain.BaseDirectory + "B.S.Data.ERP.API.Xml";
    x.IncludeXmlComments(tran, true);
});
//Mysql�����ַ���

//����ע��ִ�
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
YitIdHelper.SetIdGenerator(new IdGeneratorOptions(0));
builder.Host.UseNLog();
builder.Services.AddHttpClient();

builder.Services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
builder.Services.AddAutoMapper(typeof(ERPMapping));
var app = builder.Build();

// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
    app.UseSwagger();
    app.UseSwaggerUI();
//}

app.UseAuthorization();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.MapControllers();

app.Run();
